package com.woniuxy.food.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniuxy.food.infra.mapper.FoodPropValueMapper;
import com.woniuxy.food.infra.mapper.FoodSpuMapper;
import com.woniuxy.food.infra.model.FoodProp;
import com.woniuxy.food.infra.mapper.FoodPropMapper;
import com.woniuxy.food.infra.model.FoodPropValue;
import com.woniuxy.food.infra.model.FoodSpu;
import com.woniuxy.food.service.FoodPropService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniuxy.food.service.servicedto.ServiceFoodPropDto;
import com.woniuxy.food.service.serviceparam.InsertFoodPropServiceParam;
import exception.MallException;
import io.seata.spring.annotation.GlobalTransactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import result.CommonResultCodeEnum;

import java.util.List;

/**
 * <p>
 * 菜品属性表 服务实现类
 * </p>
 *
 * @author 刘德华
 * @since 2023-07-19
 */
@Service
public class FoodPropServiceImpl extends ServiceImpl<FoodPropMapper, FoodProp> implements FoodPropService {

    @Autowired
    private FoodPropMapper foodPropMapper;
    @Autowired
    private FoodSpuMapper foodSpuMapper;
    @Autowired
    private FoodPropValueMapper foodPropValueMapper;
    @Override
    @GlobalTransactional
    public void insertFoodProp(InsertFoodPropServiceParam param) {
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",param.getStoreId()).eq("spu_id",param.getSpuId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodProp> foodPropQueryWrapper = new QueryWrapper<>();
        foodPropQueryWrapper.eq("prop_name",param.getPropName()).eq("spu_id",param.getSpuId());
        FoodProp foodProp = foodPropMapper.selectOne(foodPropQueryWrapper);
        if (foodProp!=null) {
            throw new MallException("添加的属性名称重复", CommonResultCodeEnum.INVALID_PARAM);
        }
        FoodProp foodPropNew = BeanUtil.toBean(param, FoodProp.class);
        foodPropMapper.insert(foodPropNew);
    }

    @Override
    @GlobalTransactional
    public void deleteFoodProp(Integer storeId,Integer foodPropId) {
        FoodProp foodProp = foodPropMapper.selectById(foodPropId);
        if (foodProp==null) {
            throw new MallException("当前属性不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",storeId).eq("spu_id",foodProp.getSpuId());
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodPropValue> foodPropValueQueryWrapper = new QueryWrapper<>();
        foodPropValueQueryWrapper.eq("prop_id",foodPropId);
        List<FoodPropValue> foodPropValues = foodPropValueMapper.selectList(foodPropValueQueryWrapper);
        if (foodPropValues.size()!=0) {
            throw new MallException("当前属性下还存在属性值", CommonResultCodeEnum.INVALID_PARAM);
        }
        foodPropMapper.deleteById(foodProp);
    }

    @Override
    public List<ServiceFoodPropDto> searchFoodPropBySpuId(Integer storeId, Integer spuId) {
        QueryWrapper<FoodSpu> foodSpuQueryWrapper = new QueryWrapper<>();
        foodSpuQueryWrapper.eq("store_id",storeId).eq("spu_id",spuId);
        FoodSpu foodSpu = foodSpuMapper.selectOne(foodSpuQueryWrapper);
        if (foodSpu==null) {
            throw new MallException("当前菜品spu不存在", CommonResultCodeEnum.INVALID_PARAM);
        }
        QueryWrapper<FoodProp> foodPropQueryWrapper = new QueryWrapper<>();
        foodPropQueryWrapper.eq("spu_id",spuId);
        List<FoodProp> foodProps = foodPropMapper.selectList(foodPropQueryWrapper);
        List<ServiceFoodPropDto> serviceFoodPropDtos = BeanUtil.copyToList(foodProps, ServiceFoodPropDto.class);
        return serviceFoodPropDtos;
    }
}
